package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("CrawlerdetectstatusController")
public class CrawlerdetectstatusController extends CmiEmaController {

	@RequestMapping("/listCrawlerdetectstatuss")
	public ModelAndView listCrawlerdetectstatuss(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerdetectstatusPaginationService.updatePagination(request, pagination);
		Set<Crawlerdetectstatus> crawlerdetectstatuss = crawlerdetectstatusPaginationService
				.getPaginationCrawlerdetectstatuss(pagination);

		mav.addObject("crawlerdetectstatuss", crawlerdetectstatuss);
		mav.addObject("entityName", "crawlerdetectstatus");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerdetectstatus/listCrawlerdetectstatuss.jsp");
		return mav;
	}

	public String listCrawlerdetectstatuss() {
		return "redirect:/listCrawlerdetectstatuss";
	}

	@RequestMapping("/viewCrawlerdetectstatus")
	public ModelAndView viewCrawlerdetectstatus(@RequestParam Integer crawlerdetectstatusId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerdetectstatus",
				crawlerdetectstatusDAO.findCrawlerdetectstatusByPrimaryKey(crawlerdetectstatusId));
		mav.setViewName("crawlerdetectstatus/viewCrawlerdetectstatus.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerdetectstatuss")
	public ModelAndView confirmDeleteCrawlerdetectstatuss(
			@RequestParam(value = "deleteCrawlerdetectstatussIds", required = true) Set<Integer> deleteCrawlerdetectstatussIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerdetectstatus> deleteCrawlerdetectstatuss = crawlerdetectstatusDAO
				.findCrawlerdetectstatussByIds(deleteCrawlerdetectstatussIds);
		Set<Crawlerdetectstatus> referencedCrawlerdetectstatuss = crawlerdetectstatusService
				.loadReferenceCrawlerdetectstatuss(deleteCrawlerdetectstatuss);
		boolean hasReferenced;
		if (referencedCrawlerdetectstatuss.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerdetectstatuss);
			hasReferenced = false;
		} else {
			deleteCrawlerdetectstatuss.removeAll(referencedCrawlerdetectstatuss);
			mav.addObject("referencedEntitys", referencedCrawlerdetectstatuss);
			mav.addObject("deleteEntitys", deleteCrawlerdetectstatuss);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerdetectstatus");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerdetectstatuss");
		return mav;
	}

	@RequestMapping("/deleteCrawlerdetectstatus")
	public String deleteCrawlerdetectstatus(@RequestParam Integer crawlerdetectstatusId) {
		Crawlerdetectstatus crawlerdetectstatus = crawlerdetectstatusDAO
				.findCrawlerdetectstatusByPrimaryKey(crawlerdetectstatusId);
		crawlerdetectstatusService.deleteCrawlerdetectstatus(crawlerdetectstatus);
		return "forward:/listCrawlerdetectstatuss";
	}

	@RequestMapping("/confirmDeleteCrawlerdetectstatus")
	public ModelAndView confirmDeleteCrawlerdetectstatus(@RequestParam Integer crawlerdetectstatusId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerdetectstatus",
				crawlerdetectstatusDAO.findCrawlerdetectstatusByPrimaryKey(crawlerdetectstatusId));

		mav.setViewName("crawlerdetectstatus/deleteCrawlerdetectstatus.jsp");

		return mav;
	}

	@RequestMapping("/deleteCrawlerdetectstatuss")
	public String deleteCrawlerdetectstatuss(
			@RequestParam(value = "deleteCrawlerdetectstatussId", required = true) Set<Integer> deleteCrawlerdetectstatussId) {
		crawlerdetectstatusService.deleteCrawlerdetectstatuss(deleteCrawlerdetectstatussId);
		return "forward:/listCrawlerdetectstatuss";
	}

	@RequestMapping("/editCrawlerdetectstatus")
	public ModelAndView editCrawlerdetectstatus(@RequestParam Integer crawlerdetectstatusId) {

		ModelAndView mav = new ModelAndView();

		Crawlerdetectstatus crawlerdetectstatus = crawlerdetectstatusDAO
				.findCrawlerdetectstatusByPrimaryKey(crawlerdetectstatusId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerdetectstatus", crawlerdetectstatus);

		// 把所有的引用crawlerdetectstatus对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerdetectstatusService.loadCrawlerdetectstatusObjectReferencedSet(crawlerdetectstatus));

		// 将编辑页面crawlerdetectstatus添加到mav中
		mav.setViewName("crawlerdetectstatus/editCrawlerdetectstatus.jsp");
		return mav;
	}

	@RequestMapping("/newCrawlerdetectstatus")
	public ModelAndView newCrawlerdetectstatus() {
		ModelAndView mav = new ModelAndView();
		Crawlerdetectstatus crawlerdetectstatus = new Crawlerdetectstatus();

		mav.addObject("crawlerdetectstatus", crawlerdetectstatus);

		// 把所有的引用crawlerdetectstatus对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerdetectstatusService.loadCrawlerdetectstatusObjectReferencedSet(crawlerdetectstatus));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerdetectstatus/editCrawlerdetectstatus.jsp");

		return mav;
	}

	@RequestMapping("/saveCrawlerdetectstatus")
	public ModelAndView saveCrawlerdetectstatus(@ModelAttribute Crawlerdetectstatus crawlerdetectstatus) {

		ModelAndView mav = new ModelAndView();

		Crawlerdetectstatus existingCrawlerdetectstatus = CmiHibernateProxyUtil.deproxy(
				crawlerdetectstatusDAO.findCrawlerdetectstatusByPhysicalPrimaryKey(crawlerdetectstatus.getName()));

		// 新建记录
		if (crawlerdetectstatus.getId() == null) {
			if (crawlerdetectstatus.equals(existingCrawlerdetectstatus)) {

				mav.addAllObjects(
						crawlerdetectstatusService.loadCrawlerdetectstatusClassReferencedSet(crawlerdetectstatus));
				// mav = editContinue(mav, crawlerdetectstatus);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerdetectstatus/editCrawlerdetectstatus.jsp");
			} else {
				crawlerdetectstatusService.saveCrawlerdetectstatus(crawlerdetectstatus);
				mav.setViewName("forward:/listCrawlerdetectstatuss");
			}
		}
		// 编辑记录
		else {
			Crawlerdetectstatus originCrawlerdetectstatus = CmiHibernateProxyUtil
					.deproxy(crawlerdetectstatusDAO.findCrawlerdetectstatusByPrimaryKey(crawlerdetectstatus.getId()));
			if (!crawlerdetectstatus.equals(originCrawlerdetectstatus)
					&& crawlerdetectstatus.equals(existingCrawlerdetectstatus)) {
				mav.addAllObjects(
						crawlerdetectstatusService.loadCrawlerdetectstatusClassReferencedSet(crawlerdetectstatus));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerdetectstatus/editCrawlerdetectstatus.jsp");
			} else {
				crawlerdetectstatusService.saveCrawlerdetectstatus(crawlerdetectstatus);
				mav.setViewName("forward:/listCrawlerdetectstatuss");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerdetectstatusController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
